package com.woniuxy.food.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.food.infra.mapper.FoodSkuMapper;
import com.woniuxy.food.infra.mapper.FoodSpuMapper;
import com.woniuxy.food.infra.model.FoodPic;
import com.woniuxy.food.infra.mapper.FoodPicMapper;
import com.woniuxy.food.infra.model.FoodSku;
import com.woniuxy.food.infra.model.FoodSpu;
import com.woniuxy.food.service.FoodPicService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.food.service.servicedto.FoodPicServiceDto;
import com.woniuxy.food.service.util.CheckImage;
import com.woniuxy.food.service.util.OSSUtil;
import exception.MallException;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import result.CommonResultCodeEnum;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 菜品图片地址表 服务实现类
 * </p>
 *
 * @author 刘德华
 * @since 2023-07-28
 */
@Service
public class FoodPicServiceImpl extends ServiceImpl<FoodPicMapper, FoodPic> implements FoodPicService {

    @Autowired
    private FoodPicMapper foodPicMapper;
    @Autowired
    private FoodSpuMapper foodSpuMapper;
    @Autowired
    private FoodSkuMapper foodSkuMapper;
    @Autowired
    private OSSUtil ossUtil;
    @Override
    @GlobalTransactional
    public void insertFoodPic(Integer storeId, String file) {
        FoodPic foodPic = new FoodPic();
        foodPic.setStoreId(storeId);
        foodPic.setPicPath(file);
        save(foodPic);
    }

    @Override
    @GlobalTransactional
    public void deleteFoodPic(Integer storeId, Integer foodPicId) {
        QueryWrapper<FoodPic> foodPicQueryWrapper = new QueryWrapper<>();
        foodPicQueryWrapper.eq("store_id",storeId).eq("food_pic_id",foodPicId);
        FoodPic foodPic = foodPicMapper.selectOne(foodPicQueryWrapper);
        if (foodPic==null) {
            throw new MallException("当前图片不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        QueryWrapper<FoodSpu> foodSpuQueryWrapper = new QueryWrapper<>();
        foodSpuQueryWrapper.eq("main_pic",foodPic.getPicPath());
        List<FoodSpu> foodSpus = foodSpuMapper.selectList(foodSpuQueryWrapper);
        if (foodSpus.size()!=0) {
            throw new MallException("当前图片正在被使用", CommonResultCodeEnum.INVALID_PARAM);
        }
        QueryWrapper<FoodSku> foodSkuQueryWrapper = new QueryWrapper<>();
        foodSkuQueryWrapper.eq("main_pic",foodPic.getPicPath());
        List<FoodSku> foodSkus = foodSkuMapper.selectList(foodSkuQueryWrapper);
        if (foodSkus.size()!=0) {
            throw new MallException("当前图片正在被使用", CommonResultCodeEnum.INVALID_PARAM);
        }
        deleteImage(foodPic.getPicPath());
        foodPicMapper.deleteById(foodPicId);
    }

    @Override
    public List<FoodPicServiceDto> searchFoodPic(Integer storeId) {
        QueryWrapper<FoodPic> foodPicQueryWrapper = new QueryWrapper<>();
        foodPicQueryWrapper.eq("store_id",storeId);
        List<FoodPic> foodPics = foodPicMapper.selectList(foodPicQueryWrapper);
        List<FoodPicServiceDto> foodPicServiceDtos = BeanUtil.copyToList(foodPics, FoodPicServiceDto.class);
        return foodPicServiceDtos;
    }

    @Override
    public FoodPicServiceDto searchFoodPicById(Integer storeId, Integer foodPicId) {
        QueryWrapper<FoodPic> foodPicQueryWrapper = new QueryWrapper<>();
        foodPicQueryWrapper.eq("store_id",storeId).eq("food_pic_id",foodPicId);
        FoodPic foodPic = foodPicMapper.selectOne(foodPicQueryWrapper);
        FoodPicServiceDto foodPicServiceDto = BeanUtil.toBean(foodPic, FoodPicServiceDto.class);
        return foodPicServiceDto;
    }

    //删除图片
    public void deleteImage(String picPath){
        String name = picPath.substring(picPath.lastIndexOf("/"));
        ossUtil.deleteFile("zjr-bucket",name.substring(1));
    }
}
